var util = require('../../libs/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    changeVehicleRole:false,
    dispatchVehicleRole:false,
    verifyVehicle:true
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    // wx.showLoading({
    //   title: '请稍候...',
    // });
    //更新数据
    that.setData({
      vehicle_id: e.id,
      tab: e.tab||0,
      semitrailer:e.semitrailer,
      role_default:app.globalData.role_default
    });
    
    wx.setNavigationBarTitle({ "title": "车辆档案" });
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight - 40
        });
      }
    });
    this.loadData();
  },
  loadData: function () {
    var that = this;
     util.getData("/wechat/vehicle/getVehicleDetail", { vehicle_id: that.data.vehicle_id }, function (res) {
      console.log(res.data)
      if (res.data.success) {
        var vehicle_data = res.data.data; //车辆基本信息
        wx.setNavigationBarTitle({ "title": "车辆档案--" + vehicle_data.plate });
          that.setData({
            vehicle_data: vehicle_data
          });
        if (app.globalData.admin == true || (app.globalData.coAdmin == true && vehicle_data.CO_ID == app.globalData.CO_ID) 
          || (app.globalData.role_default == 'fleet' && vehicle_data.CO_ID == app.globalData.CO_ID)) {//|| (app.globalData.user_id == vehicle_data.user_id && vehicle_data.CO_ID == 0)
          that.setData({
            dispatchVehicleRole: true
          })
        }
        if ((app.globalData.CO_ID != 0 && vehicle_data.CO_ID == app.globalData.CO_ID) || (app.globalData.user_id  == vehicle_data.user_id && vehicle_data.CO_ID == 0)) {
          that.setData({
            changeVehicleRole: true
          })
        }
        if (app.globalData.user_id == vehicle_data.user_id && vehicle_data.CO_ID == 0) {
          that.setData({
            verifyVehicle: false
          })
        }
        // if(vehicle_data.changeCompany){
        //   var roleData = app.globalData.roleData;
        //   var isFleet = false;
        //   for(var i=0;i<roleData.length;i++){
            
        //   }
        //   that.setData({
        //     modalName:'requestAddCompanyModal',
        //     changeCompany:vehicle_data.changeCompany,
        //     scrollHeight: that.data.scrollHeight - 205
        //   })
        // }
      }
    });
  },
  hideModal2(e) {
    this.setData({
      modalName: null,
      scrollHeight: this.data.scrollHeight + 205
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  confirmAddInCompany:function(e){
    var that = this;
    wx.showModal({
      title: '车辆申请加入企业',
      content: '是否确认车辆加入企业？确认后车辆和司机将迁移到企业。',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '请稍候...',
          });
          util.postData("/atapi/vehicle/vehicle/confirmChangeCompany", {
            id: that.data.vehicle_id,
            CO_ID:app.globalData.CO_ID,
            CO_NAME:app.globalData.CO_NAME,
            user_id:app.globalData.user_id,
            truename:app.globalData.truename,
            changeCompany:that.data.changeCompany
          }, function (res, error) {
            console.log(res); console.log(error)
            wx.hideLoading();
            if (error) {
              wx.showToast({
                title: '审核失败',
                image: '/images/wrong.png',
                duration: 3000
              });
              return false;
            }
            if (res.data.success) {
              wx.showToast({
                title: "审核通过",
                icon: 'success',
                success: function () {
                  setTimeout(function () {
                    that.setData({
                      scrollHeight: that.data.scrollHeight - 205
                    })
                    wx.redirectTo({
                      url: '/pages/vehicle/vehicle_detail?id=' + that.data.vehicle_data.ID
                    })
                  }, 3000);
                }
              });
            } else {
              wx.showToast({
                title: '审核失败',
                image: '/images/wrong.png',
                duration: 3000
              });
              return false;
            }
          });
        } else if (res.cancel) {
        }
      }
    })
  },
  cancelAddInCompany:function(e){
    var  that = this;
    wx.showModal({
      title: '车辆申请加入企业',
      content: '确认不同意本车辆加入企业吗？',
      success: function (res) {
        if (res.confirm) {
          util.postData("/atapi/vehicle/vehicle/cancelChangeCompany", {
            id: that.data.vehicle_id
          }, function (res, error) {
            console.log(res); console.log(error)
            wx.hideLoading();
            if (error) {
              wx.showToast({
                title: '审核失败',
                image: '/images/wrong.png',
                duration: 3000
              });
              return false;
            }
            if (res.data.success) {
              wx.showToast({
                title: "审核成功",
                icon: 'success',
                success: function () {
                  setTimeout(function () {
                    that.setData({
                      scrollHeight: that.data.scrollHeight - 205
                    })
                    wx.redirectTo({
                      url: '/pages/vehicle/vehicle_detail?id=' + that.data.vehicle_data.ID
                    })
                  }, 3000);
                }
              });
            } else {
              wx.showToast({
                title: '审核失败',
                image: '/images/wrong.png',
                duration: 3000
              });
              return false;
            }
          });
          that.setData({
            modalName:null,
            scrollHeight: that.data.scrollHeight + 205
          });
        }else{
          
        }
      }
    });
  },
  verifyVehicle: function (e) {
    var vehicle_data = this.data.vehicle_data;
    if (vehicle_data.ischeck == '1') {
      wx.showModal({
        title: '车辆审核',
        content: '车辆（' + vehicle_data.plate + '）已审核通过',
        showCancel: false,
        success: function (res) {
        }
      })
      return false;
    } else {
      wx.showModal({
        title: '车辆审核',
        content: '是否确认审核通过',
        success: function (res) {
          if (res.confirm) {
            wx.showLoading({
              title: '请稍候...',
            });
            util.putData("verifyVehicle", {
              id: vehicle_data.ID
            }, function (res, error) {
              console.log(res); console.log(error)
              wx.hideLoading();
              if (error) {
                wx.showToast({
                  title: '审核失败',
                  image: '/images/wrong.png',
                  duration: 3000
                });
                return false;
              }
              if (res.data.success) {
                wx.showToast({
                  title: "审核通过",
                  icon: 'success',
                  success: function () {
                    setTimeout(function () {
                      wx.redirectTo({
                        url: '/pages/vehicle/vehicle_detail?id=' + vehicle_data.ID
                      })
                    }, 3000);
                  }
                });
              } else {
                wx.showToast({
                  title: '审核失败',
                  image: '/images/wrong.png',
                  duration: 3000
                });
                return false;
              }
            });
          } else if (res.cancel) {
          }
        }
      })
    }
  },
  changeVehicleInfo:function(){
    wx.setStorage({
      key: "vehicleData",
      data: this.data.vehicle_data
    })
     wx.navigateTo({
       url: '/pages/vehicle/vehicle_edit?id=' + this.data.vehicle_data.ID
     })
  },
  previewImage:function(e){
    var url = e.currentTarget.dataset.url;
    console.log(e)
    if (url){
      wx.previewImage({
        urls: [url]
      })
    }
  },
  changeCompany:function(){
    wx.navigateTo({
      url: '/pages/fleet/selectCompany'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.getStorage({
      key: 'vehicleEditBack',
      success(res) {
        that.loadData();
        wx.removeStorageSync('vehicleEditBack')
      }
    });
    wx.getStorage({
      key: 'companyData',
      success(res) {
        var CO_ID = res.data.value, CO_NAME = res.data.CO_NAME;
        wx.removeStorageSync('companyData');
        if (that.data.vehicle_data.CO_ID == CO_ID) {
          wx.showModal({
            title: '温馨提示',
            content: '所选企业和当前企业相同',
            success(res) {
            }
          });
          return false;
        }
        wx.showModal({
          title: '温馨提示',
          content: '确定要将车辆的企业改为归属：' + CO_NAME + '吗？',
          success(res) {
            if (res.confirm) {
              wx.showLoading({
                title: '请稍候...',
              });
              util.putData("/wechat/vehicle/changeCompany", { ID: that.data.vehicle_data.ID, CO_ID: CO_ID}, function (res, error) {
                console.log(res); console.log(error)
                wx.hideLoading();
                if (error) {
                  wx.showToast({
                    title: res.data.data,
                    image: '/images/wrong.png',
                    duration: 3000
                  });
                  return false;
                }
                if (res.data.success) {
                  wx.showToast({
                    title: "修改成功",
                    icon: "success",
                    duration: 2000
                  });
                  setTimeout(function () {
                    that.loadData();
                  }, 2000)
                } else if (res.data.success == false) {
                  wx.showToast({
                    title: res.data.data,
                    image: '/images/wrong.png',
                    duration: 3000,
                    mask: true
                  });
                }
              });
            } else if (res.cancel) {
            }
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})